package com.example.xdemo.slice;

import com.example.xdemo.MyAdapter;
import com.example.xdemo.ResourceTable;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.ArrayList;

public class LinearAbilitySlice extends AbilitySlice {
    private XRecyclerView mRecyclerView;
    private MyAdapter<String> mAdapter;
    private ArrayList<String> listData;
    private int refreshTime = 0;
    private int times = 0;

    private Context mContext;

    private final EventHandler handler = new EventHandler(EventRunner.getMainEventRunner());

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_linear);
        mContext = getContext();
        initView();
    }

    private void initView(){
        mRecyclerView = (XRecyclerView)findComponentById(ResourceTable.Id_abt_linear_xrecyclerview);

        mRecyclerView.setRefreshProgressStyle(ProgressStyle.SysProgress);
        mRecyclerView.getDefaultRefreshHeaderView().setRefreshTimeVisible(true);
        mRecyclerView.getDefaultRefreshHeaderView().setIndicatorColor(Color.RED.getValue());

        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.SysProgress);
        mRecyclerView.setLoadingMoreProgressIndicatorColor(Color.RED.getValue());
        mRecyclerView.setArrowImageView(ResourceTable.Media_iconfont_downgrey);

        LayoutScatter scatter = LayoutScatter.getInstance(mContext);
        Component header = scatter.parse(ResourceTable.Layout_recyclerview_header, null, false);
        header.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new ToastDialog(mContext).setText("这是头部").show();
            }
        });

        mRecyclerView.addHeaderView(header);
        mRecyclerView.getDefaultFootView().setLoadingHint("自定义加载中提示");
        mRecyclerView.getDefaultFootView().setNoMoreHint("自定义加载完毕提示");

        final int itemLimit = 5;
        mRecyclerView.setLimitNumberToCallLoadMore(2);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                handler.removeTask(refreshRunnable);
                handler.postTask(refreshRunnable, 1000);
            }

            @Override
            public void onLoadMore() {

                if(times < 2){
                    new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
                        @Override
                        public void run() {
                            for(int i = 0; i < itemLimit ;i++){
                                listData.add("item" + (1 + listData.size() ) );
                            }
                            if(mRecyclerView != null) {
                                mRecyclerView.loadMoreComplete();
                                mAdapter.notifyDataChanged();
                            }
                        }
                    },1000);
                }else {
                    new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
                        @Override
                        public void run() {
                            for(int i = 0; i < itemLimit ;i++){
                                listData.add("item" + (1 + listData.size() ) );
                            }
                            if(mRecyclerView != null) {
                                mRecyclerView.setNoMore(true);
                                mAdapter.notifyDataChanged();
                            }
                        }
                    },1000);
                }
                times++;
            }
        });

        listData = new ArrayList<>();
        mAdapter = new MyAdapter<>(getContext(),listData,ResourceTable.Layout_list_item);

        mAdapter.setOnItemClickListener(new XRecyclerView.RecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Component component, int position) {

                new ToastDialog(getContext()).setText("nihao"+position).show();
            }
        });
        mRecyclerView.setItemProvider(mAdapter);
        mRecyclerView.refresh();
    }

    private final Runnable refreshRunnable = new Runnable() {
        @Override
        public void run() {
            refreshTime ++;
            times = 0;
            listData.clear();
            for(int i = 0; i < 20 ;i++){
                listData.add("item" + i + "after " + refreshTime + " times of refresh");
            }
            mAdapter.notifyDataChanged();
            if(mRecyclerView != null)
                mRecyclerView.refreshComplete();
        }
    };
}
